package com.rcache.core.persistence.utils;

import com.rcache.core.Element;
import com.rcache.core.cache.Rcache;
import org.apache.log4j.Logger;

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FileUtils {
    static Logger LOG=Logger.getLogger(FileUtils.class);
    public static Element getFile(String path, Object ElementKey) throws IOException, ClassNotFoundException {
        File file = new File(path + "/" + ElementKey + ".cache");
        List<File> list = new ArrayList<>();
        if (file.exists()) {
            LOG.debug("文件存在");
            File[] filelist = file.listFiles();
            for (File files : filelist) {
                list.add(files);
            }
            Element element = null;
            LOG.debug("本地文件数量：" + list.size());
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
                            list.get(i)));
                    element = (Element) ois.readObject();
                    LOG.debug("反实例化成功");
                    ois.close();
                }
                return element;
            }
        }
        return null;

    }

    public static Boolean putFlie(String path, Element element) throws IOException, ClassNotFoundException {
        File file = new File(path + "/"+element.getObjectKey() + ".cache");
        File file1 = new File(path);
        LOG.debug(path);
        if (!file.exists() && !file.isDirectory()) {
            file1.mkdirs();
        }
        if (!file.exists() && !file.isDirectory()) {
            LOG.debug("文件夹不存在，已创建");
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
            out.writeObject(element);
            LOG.debug("对象序列化成功");
            out.close();
        } else {
            LOG.debug("文件夹存在");
        }
        return true;
    }

    public static int numberOfFiles(String path) {
        File folder = new File(path);
        File[] list = folder.listFiles();
        int fileCount = 0;
        long length = 0;
        for (File file : list) {
            if (file.isFile()) {
                fileCount++;
                length += file.length();
            }
        }
        LOG.debug(" 缓存文件的数目: " + fileCount);
        return fileCount;
    }

    public static boolean deleteFile(String path) {
        //创建一个文件对象
        File file = new File(path);
        // 删除文件
        boolean value = file.delete();
        if(value) {
            LOG.debug("缓存文件成功清除.");
            return true;
        }
        else {
            LOG.debug("文件不存在");
            return false;
        }
    }
    public static boolean tryRestore(String path,Rcache cache) throws IOException, ClassNotFoundException {
        File file = new File(path);
        List<File> list = new ArrayList<>();
        if (file.exists()) {
            LOG.debug("文件存在");
            File[] filelist = file.listFiles();
            for (File files : filelist) {
                list.add(files);
            }
            LOG.debug("文件数量：" + list.size());
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(list.get(i)));
                    Element value = (Element) ois.readObject();
                    Object key = value.getObjectKey();
                    LOG.debug("恢复数据："+value);
                    cache.put(key,value.getObjectValue());
                    ois.close();
                }
                return true;
            }
        }
        return false;

    }


}
